package com.first.controller;


import com.first.entity.ClassEntity;
import com.first.service.ClassService;
import com.first.util.R;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @GetMapping("/classList")
    public R getClassList()
    {
        List<ClassEntity> classList = classService.getClassList();
        if(classList!=null)
        {
            //System.out.println("获取成功!");
            return R.ok().data("list",classList);
        }
        else
        {
            System.out.println("获取失败!");
            return R.error();
        }
    }

    @PostMapping("/update/{className}/{classType}/{classNumber}/{id}")
    public R updateClass(@PathVariable("className") String className,@PathVariable("classType") String classType,@PathVariable("classNumber") Integer classNumber,@PathVariable("id") Integer id)
    {
        int i = classService.updateClass(className, classType, classNumber, id);
        if(i>0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    public R deleteClass(@PathVariable("id") Integer id)
    {
        int i = classService.deleteClass(id);
        if(i>0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @PostMapping("/insert")
    public R insertClass(String className,String classType,Integer classNumber)
    {
        int i = classService.insertClass(className, classType, classNumber);
        if(i>0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

}
